/* internal/js/patron/maintain.js */

/* Common */

var PATRON_SERVLET_PATH = '/ordrs/internal/maintain-patron';

/* END Common */


/* Create */

function createAccount () {
	createAccountValidate();
}

//done
function createAccountValidate () {
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var phone = document.getElementById('phone').value;
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var email = document.getElementById('email').value;
	var street = document.getElementById('street').value;
	var city = document.getElementById('city').value;
	var province = document.getElementById('province').value;
	var postalcode = document.getElementById('postalcode').value;

	var valid = true;

	if (firstname == '') { //left blank
		valid = false;
		showFieldRequired('firstnameMessage');
	}

	if (lastname == '') { //left blank
		valid = false;
		showFieldRequired('lastnameMessage');
	}
	if (phone == '') { //left blank
		valid = false;
		showFieldRequired('phoneMessage');
	}

	if (username == '') { //left blank
		valid = false;
		showFieldRequired('usernameMessage');
	}
	if (password == '') { //left blank
		valid = false;
		showFieldRequired('passwordMessage');
	}

	if (email == '') { //left blank
		valid = false;
		showFieldRequired('emailMessage');
	}
	if (street == '') { //left blank
		valid = false;
		showFieldRequired('streetMessage');
	}

	if (city == '') { //left blank
		valid = false;
		showFieldRequired('cityMessage');
	}
	if (province == '') { //left blank
		valid = false;
		showFieldRequired('provinceMessage');
	}

	if (postalcode == '') { //left blank
		valid = false;
		showFieldRequired('postalcodeMessage');
	}
	
	if (valid)
		createAccountSend(firstname, lastname, phone, username, password, email,street,city,province,postalcode);
}

//done
function createAccountSend (firstname, lastname, phone, username, password, email,street,city,province,postalcode) {
	var url = PATRON_SERVLET_PATH;
	var params = 'a=' + encodeURI('c') +
	            '&firstname=' + encodeURI(firstname) +
	            '&lastname=' + encodeURI(lastname) +
	            '&phone=' + encodeURI(phone) +
	            '&username=' + encodeURI(username) +
	            '&password=' + encodeURI(password) +
	            '&email=' + encodeURI(email) +
	            '&street=' + encodeURI(street) +
	            '&city=' + encodeURI(city) +
	            '&province=' + encodeURI(province) +
	            '&postalcode=' + encodeURI(postalcode);
	
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				createAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				createAccountReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function createAccountReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		listAllSend();
		showDialog('Patron Account is created.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Create */


/* Report */

function reportAccount () {
	reportAccountValidate();
}

//done
function reportAccountValidate () {
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var username = document.getElementById('username').value;
	var city = document.getElementById('city').value;
	var province = document.getElementById('province').value;
	var phone = document.getElementById('phone').value;
	var postalcode = document.getElementById('postalcode').value;
	var email = document.getElementById('email').value;

	//TODO always valid because available is always a search field THIS IS WRONG NOW
	reportAccountSend(firstname, lastname, phone,username,email,city,province, postalcode);
}

//done
function reportAccountSend(firstname, lastname, phone,username,email,city,province, postalcode) {
	var url = PATRON_SERVLET_PATH;
	var params = 'a=' + encodeURI('r') +
	            '&firstname=' + encodeURI(firstname) +
		        '&lastname=' + encodeURI(lastname) +
		        '&phone=' + encodeURI(phone) +
		        '&username=' + encodeURI(username) +
		        '&email=' + encodeURI(email) +
		        '&city=' + encodeURI(city) +
		        '&province=' + encodeURI(province) +
	            '&postalcode=' + encodeURI(postalcode);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				reportAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				reportAccountReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function reportAccountReceive (xml) {
	if (xml.getElementsByTagName('patrons').length > 0) {
		var patrons = xml.getElementsByTagName('patrons')[0];
		showResults(buildTable(patrons));
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END Report*/


/* Update */

function updateAccount () {
	updateAccountValidate();
}

function updateAccountValidate () {
	var id = document.getElementById('id').value;
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var phone = document.getElementById('phone').value;
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var email = document.getElementById('email').value;
	var street = document.getElementById('street').value;
	var city = document.getElementById('city').value;
	var province = document.getElementById('province').value;
	var postalcode = document.getElementById('postalcode').value;

	var valid = true;

	if (firstname == '') { //left blank
		valid = false;
		showFieldRequired('firstnameMessage');
	}

	if (lastname == '') { //left blank
		valid = false;
		showFieldRequired('lastnameMessage');
	}
	if (phone == '') { //left blank
		valid = false;
		showFieldRequired('phoneMessage');
	}

	if (username == '') { //left blank
		valid = false;
		showFieldRequired('usernameMessage');
	}
	if(password == '')
	{

	}else if(password == 'j8&#4i^b/1!c')
	{
		password = '';
	}

	if (email == '') { //left blank
		valid = false;
		showFieldRequired('emailMessage');
	}
	if (street == '') { //left blank
		valid = false;
		showFieldRequired('streetMessage');
	}

	if (city == '') { //left blank
		valid = false;
		showFieldRequired('cityMessage');
	}
	if (province == '') { //left blank
		valid = false;
		showFieldRequired('provinceMessage');
	}

	if (postalcode == '') { //left blank
		valid = false;
		showFieldRequired('postalcodeMessage');
	}

	if (valid)
		updateAccountSend(id,firstname, lastname, phone, username, password, email,street,city,province,postalcode);
}

//really done
function updateAccountSend (id,firstname, lastname, phone, username, password, email,street,city,province,postalcode) {
	var url = PATRON_SERVLET_PATH;
	var params = 'a=' + encodeURI('u') +
	            '&id=' + encodeURI(id) +
	            '&firstname=' + encodeURI(firstname) +
	            '&lastname=' + encodeURI(lastname) +
	            '&phone=' + encodeURI(phone) +
	            '&username=' + encodeURI(username) +
	            '&password=' + encodeURI(password) +
	            '&email=' + encodeURI(email) +
	            '&street=' + encodeURI(street) +
	            '&city=' + encodeURI(city) +
	            '&province=' + encodeURI(province) +
	            '&postalcode=' + encodeURI(postalcode);
	
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				updateAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				updateAccountReceive(encodeErrorXML('-1', 'Cannot find server.'), false);
			}
		}
	};
	sendPost(req, url, params);
}


function updateAccountReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		listAllSend();
		showDialog('Patron Account updated.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Update */


/* Delete */

function deleteAccount () {
	deleteAccountValidate();
}

function deleteAccountValidate () {
	var id = document.getElementById('id').value;
	if (id == '') {
		//no account selected
		showDialog('No Patron Account is selected for deletion.');
	} else {
		//account selected
		deleteAccountSend(id);
	}
}


function deleteAccountSend (id) {
	var url = PATRON_SERVLET_PATH;
	var params = 'a=' + encodeURI('d') +
	            '&id=' + encodeURI(id);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				deleteAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				deleteAccountReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function deleteAccountReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		resetFields();
		listAllSend();
		showDialog('Patron Account Deleted.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Delete */


/* List All */

function listAll() {
	listAllSend();
}

function listAllSend () {
	var url = PATRON_SERVLET_PATH;
	var params = 'a=' + encodeURI('l');

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listAllReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listAllReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listAllReceive (xml) {
	if (xml.getElementsByTagName('patrons').length > 0) {
		var patrons = xml.getElementsByTagName('patrons')[0];
		showResults(buildTable(patrons));
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List All */

